import 'package:flutter/material.dart' hide Image;
import 'dart:math';
import 'dart:ui';
import 'package:flutter_fun_zjversion/config/resource_manager.dart';
import 'package:flutter_fun_zjversion/utils/Uiutils.dart';

class  TurntablePainter extends CustomPainter {
  var colors = [
    Colors.white,
    Colors.amberAccent,
    Colors.red,
    Colors.green,
    Colors.blue,
    Colors.cyan
  ];
  var width;
  var height;
  List<ItemTurntableBean> list = const [];
  Image image;
  TurntablePainter(this.list);

  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = new Paint();
    paint.style = PaintingStyle.fill;
    width = size.width;
    height = size.height;
    canvas.translate(width / 2, height / 2);
    _drawArc(canvas, size, paint);
  }


  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return true;
  }


  void _drawArc(Canvas canvas, Size size, Paint paint) {
    double sweepAngle = 2 * pi / list.length;
    var startAngle = -pi / 2 - sweepAngle / 2;
    var sectorRectF = new Rect.fromPoints(
        Offset(-width / 2, -height / 2), Offset(width / 2, height / 2));
    for (int i = 0; i < list.length; i++) {
      paint.color = colors[i];
      canvas.drawArc(sectorRectF, startAngle, sweepAngle, true, paint);
      startAngle += sweepAngle;
    }
    for (int i = 0; i < list.length; i++) {
      canvas.save();
      _drawText(canvas, list[i],i,paint);
    }
  }


  void _drawText(Canvas canvas, ItemTurntableBean item,int i,Paint paint) {
    canvas.rotate(i *2*pi / list.length);
    TextPainter textPainter = new TextPainter(
      textAlign: TextAlign.center,
      textDirection: TextDirection.ltr,
      textWidthBasis: TextWidthBasis.parent,
      text: TextSpan(
          text: item.text,
          style: TextStyle(color: Colors.black)
      ),
    )
      ..layout(maxWidth: 2 * sin(pi / list.length) * width / 2,
          minWidth: 2 * sin(pi / list.length) * width / 2)
      ..paint(canvas, Offset(-cos(2 * pi / list.length) * width / 2,
          -sin(2 * pi / list.length) * width / 2));

    _drawImg(canvas,item,paint);
    canvas.restore();
  }


  void _drawImg(Canvas canvas, ItemTurntableBean item, Paint paint){
    if(item.image!=null){
      Image img= item.image;
      canvas.drawImage(img, Offset(-img.width/2, -100), paint);
//      canvas.drawImageRect(item.image, Rect.fromLTRB(-img.width/2, -100,img.width/2, img.height/2), Rect.fromLTRB(-img.width/2, -img.height/2,img.width/2, img.height/2),paint);
    }
  }

  void initImage(String img){
    Utils.loadImage(ImageHelper.wrapAssets(img),false).then((img){
        image = img;
    }).whenComplete((){
      print("图片加载已完成");
    });
  }
}




class ItemTurntableBean {
  String img;
  String text;
  Image image;
  ItemTurntableBean(this.img, this.text);

  Future<Image>  getImage() async{
    return Utils.loadImage(ImageHelper.wrapAssets(img),false).then((img){
      this.image=img;
      return img;
    });
  }
}